package cn.txy.system.controller;

import cn.txy.config.R;
import cn.txy.system.annotation.PreAuthorize;
import cn.txy.system.domain.Role;
import cn.txy.system.query.RoleQuery;
import cn.txy.system.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询角色",sn="role:getOne",descs = "根据ID查询角色")
    public R findById(@PathVariable("id")Long id){
        return R.ok(roleService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询角色",sn="role:getAll",descs = "查询角色")
    public R findAll(){
        return R.ok(roleService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除角色",sn="role:delOne",descs = "根据ID删除角色")
    public R deleteById(@PathVariable("id")Long id){
        roleService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "删除角色",sn="role:delAll",descs = "删除角色")
    public R batchDelete(@RequestBody List<Long> ids){
        roleService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "添加和修改角色",sn="role:saveOne",descs = "添加和修改角色")
    public R add(@RequestBody Role role){
        if(role.getId() == null){
            roleService.add(role);
        }else {
            roleService.update(role);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询角色",sn="role:page",descs = "分页查询角色")
    public R getPage(@RequestBody RoleQuery roleQuery){
        PageHelper.startPage(roleQuery.getPage(),roleQuery.getSize());
        List<Role> roles = roleService.queryPage(roleQuery);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return R.ok(pageInfo);
    }
}
